<?php

namespace app\admin\model;

use addons\epay\library\Service;
use think\Exception;
use think\Log;
use think\Model;
use traits\model\SoftDelete;
use Yansongda\Pay\Pay;

class Order extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
//        'pay_type_text',
//        'pay_time_text',
//        'status_text',
//        'refund_status_text'
    ];
    

    
    public function getPayTypeList()
    {
        return ['wechat' => __('Pay_type wechat'), 'alipay' => __('Pay_type alipay'), 'money' => __('Pay_type money'), 'give_money' => __('Pay_type give_money'), 'adapay_wx_pub' => 'adapay微信'];
    }

    public function getStatusList()
    {
        return ['-1' => __('Status -1'), '0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }

    public function getRefundStatusList()
    {
        return ['-1' => __('拒绝'),  '1' => __('待审核'),'2' => __('同意'),];
    }


    public function getPayTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
        $list = $this->getPayTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRefundStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_status']) ? $data['refund_status'] : '');
        $list = $this->getRefundStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function schedule()
    {
        return $this->belongsTo('Schedule', 'schedule_id', 'id', [], 'LEFT')->field('id,name,date,start_time,full_start_time');
    }

    public function hall()
    {
        return $this->belongsTo('Hall', 'hall_id', 'id', [], 'LEFT')->field('id,name,image');
    }

    public function film()
    {
        return $this->belongsTo('app\admin\model\film\Info', 'film_info_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->field('id,nickname,mobile,level')->setEagerlyType(0);
    }

    /**
     * 微信支付
     */
    public static function wxpay($order_id, $amount, $paytype = 'wechat', $method = 'wap', $url = '', $openid, $attach = '', $order_type = 'buy')
    {
        //测试注释
        // $amount = 0.01;
        $epay = get_addon_info('epay');

        if ($epay && $epay['state']) {
            if ($amount<=0){
                $amount = 0.01;
            }
//            $amount = 0.01;
            $request = \think\Request::instance();
            $notifyurl = $request->root(true) . '/addons/epay/api/notifyx/paytype/' . $paytype .'/order_type/'.$order_type;
            $returnurl = $method == 'wap' || $method == 'web' ? $url : $request->root(true) . '/addons/epay/api/returnx/paytype/' . $paytype;
            $params['amount'] = $amount;
            $params['orderid'] = $order_id;
            $params['type'] = $paytype;
            $params['title'] = "支付";
            $params['notifyurl'] = $notifyurl;
            $params['returnurl'] = $returnurl;
            $params['method'] = $method;
            $params['openid'] = $openid;
            $params['attach'] = $attach;

            return \addons\epay\library\Service::submitOrder($params);
        } else {
            throw new Exception("请先在后台安装并配置微信支付宝整合插件");
        }
    }

    /*
 * 订单取消
 * */
    public static function refund($order)
    {
        $out_request_no = build_order_no();
        $config = Service::getConfig('wechat');
//        halt($config);
        $pay = Pay::wechat($config)
            ->refund(
                [
//                    'out_trade_no'=>$order['id'],
                    'out_trade_no'=>$order['order_sn'],
                    'out_refund_no'=>$out_request_no,
                    'refund_fee' => $order['total_fee']*100,
                    'total_fee' => $order['total_fee']*100,
//                    'refund_fee' => 0.01*100,
//                    'total_fee' => 0.01*100,
                    'refund_desc' => '退款',
                    'type'        => 'mini'
                ]
            );
        $pay = json_decode($pay,true);
        Log::info('微信退款响应');
        Log::info($pay);
        return $pay;
    }
}
